package com.woniu.darryringmall.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.darryringmall.front.entity.*;
import com.woniu.darryringmall.front.service.AddressService;
import com.woniu.darryringmall.front.service.CartService;
import com.woniu.darryringmall.front.service.OrderService;
import com.woniu.darryringmall.front.service.ViewOrderService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController("frontCart")
@RequestMapping("front/cart")
public class CartController {

    /**
     * 进入购物车并能正确展示购物车数据
     * @return
     *
     */
    @Autowired
    private CartService cartService;


    @PostMapping("list")
    public ResponseData list (HttpSession session){
        System.out.println(session.getAttribute("user"));
        System.out.println(((User)session.getAttribute("user")).getId());
        List<CartVo> carts=cartService.carts(((User)session.getAttribute("user")).getId());
        return new ResponseData(carts);
    }

    /**
     * 获得购物车数量
     * @param session
     * @return
     */
    @PostMapping("size")
    public ResponseData size(HttpSession session){
        System.out.println(((User)session.getAttribute("user")).getId());
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("userId",((User)session.getAttribute("user")).getId());
        List<Cart> carts=cartService.list(queryWrapper);
        int cartsize=carts.size();
        return new ResponseData(cartsize);
    }

    /**
     * 把商品加入购物车
     * @param goodsId
     * @param session
     * @return
     */
    @PostMapping("add")
    public ResponseData add( int goodsId, int goodsSpecId,int nums, HttpSession session){
        User user=(User) session.getAttribute("user");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("userId",user.getId());
        queryWrapper.eq("goodsId",goodsId);
        Cart cart=cartService.getOne(queryWrapper);
        if (cart==null){
            Cart  newcart=new Cart();
            newcart.setUserId(user.getId());
            newcart.setNums(nums);
            newcart.setGoodsId(goodsId);
            newcart.setGoodsSpecId(goodsSpecId);
            newcart.setSelected("y");
            cartService.add(newcart);
        }else {
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("userId",user.getId());
            updateWrapper.eq("goodsId",goodsId);
            updateWrapper.eq("goodsSpecId",goodsSpecId);
            updateWrapper.set("nums",nums);
            cartService.update(queryWrapper);
        }
        return new ResponseData(200,"添加购物车成功！");
    }

    /**
     * 删除购物车
     * @param id
     * @return
     */
    @PostMapping("delete")
    public ResponseData delete( int id){
        cartService.delete(id);
        return new ResponseData(200,"删除购物车成功！");
    }

    /**
     * 修改购物车中商品的数量
     * @param id
     * @param nums
     * @return
     */
    @PostMapping("update")
    public ResponseData update( int id, int nums){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("nums",nums);
        cartService.update(updateWrapper);
        return ResponseUtils.SUCCESS;
    }
}
